<?php
/**
 * @Author: Marte
 * @Date:   2020-06-17 18:45:20
 * @Last Modified by:   Marte
 * @Last Modified time: 2020-06-19 16:10:54
 */


//yield是干什么的?
//当程序运行到yield的时候,当前程序就唤起协程记录上下文,起中断跟占位作用。

function consumer(){
    while(true){
        $receive = yield; // 设置断点
        echo 'consumer '.$receive,PHP_EOL;
        yield '200,ok!';
    }
};

function producer(Generator $generator){
    echo 'start produce',PHP_EOL,'-----',PHP_EOL;
    $num = 0;
    while(true){
        $num++;
        echo 'produce num '.$num,PHP_EOL;
        $generator->send('num '.$num); // 生产后通知 consumer 消费
        $msg = $generator->current();
        $generator->next();
        echo 'consumer return: '.$msg.'...'."\n";
        usleep(500 * 1000);
    }
    echo '------',PHP_EOL,'produce end';
};

$consumer = consumer();
producer($consumer);